package com.demo1;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class DruidTest1 {
    public static void main(String[] args) throws Exception {
        //获取配置文件 流对象
        InputStream is = DruidTest1.class.getClassLoader().getResourceAsStream("druid.properties");

        //1.Properties集合
        Properties prop= new Properties();
        prop.load(is);
        //2.连接 连接池 获取 数据库连接对象  返回一个连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        //3.连接池对象 获取 连接使用
        Connection conn = dataSource.getConnection();

        PreparedStatement ps = conn.prepareStatement("select * from stu");

        ResultSet rs = ps.executeQuery();
        while (rs.next()){
            System.out.println(rs.getInt("id")+rs.getString("name")
                    +rs.getString("password"));
        }
        //释放之后 重新归还
        rs.close();
        ps.close();
        conn.close();
    }
}
